package cn.chuanw.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.chuanw.wx.annotation.RequiresPermissionsDesc;
import cn.chuanw.core.util.RandomNumberUtil;
import cn.chuanw.core.util.ResponseUtil;
import cn.chuanw.db.domain.WHouseInfo;
import cn.chuanw.db.domain.WRoomInfo;
import cn.chuanw.db.service.IncomeRentRecordService;
import cn.chuanw.db.service.WHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/house")
@Validated
public class WHouseController {
    private final Log logger = LogFactory.getLog(WHouseController.class);

    @Autowired
    private WHouseService wHouseService;
    @Autowired
    private IncomeRentRecordService incomeRentRecordService;
    
    @RequiresPermissions("admin:house:list")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房屋信息"}, button = "查询")
    @GetMapping("/list")
    public Object list(
    		String community,
    		String fullName,
    		String landlord,
    		Integer leaseStatus,
    		String playTime,
    		Integer	playFlag,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        List<WHouseInfo> wHouseInfos = wHouseService.querySelective(community,fullName,landlord,leaseStatus,playTime,playFlag, page, limit);
        //this.houseplayUpdate();
        return ResponseUtil.okList(wHouseInfos);
    }

    
    @RequiresPermissions("admin:house:add")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房屋信息"}, button = "新增")
    @PostMapping("/add")
    public Object add(@RequestBody WHouseInfo wHouseInfo) {
    	wHouseInfo.setCreateTime(LocalDateTime.now());
    	wHouseInfo.setUpdateTime(LocalDateTime.now());
    	wHouseInfo.setId(RandomNumberUtil.getStringRandom(12));
    	wHouseService.addWHouseInfo(wHouseInfo);
        return ResponseUtil.ok(wHouseInfo);
    }


    @RequiresPermissions("admin:house:update")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房屋信息"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody WHouseInfo wHouseInfo) {
    	wHouseInfo.setUpdateTime(LocalDateTime.now());
    	wHouseService.updateById(wHouseInfo);
        return ResponseUtil.ok(wHouseInfo);
    }

    @RequiresPermissions("admin:houseplay:update")
    @RequiresPermissionsDesc(menu = {"公寓管理", "房屋信息"}, button = "刷新")
    @PostMapping("/houseplay")
    public Object houseplayUpdate() {
    	  List<WHouseInfo> wHouseInfos = wHouseService.playFlagUpdate();	
        return ResponseUtil.okList(wHouseInfos);
    }
 
    //查询 所有房屋名称
    @GetMapping("/options")
    public Object options( ) {
        List<WHouseInfo> wHouseInfos = wHouseService.queryHouse();
        List<Map<String, Object>> options = new ArrayList<>(wHouseInfos.size());
        for (WHouseInfo wHouseInfo : wHouseInfos) {
            Map<String, Object> option = new HashMap<>(2);
            option.put("value", wHouseInfo.getId());
            option.put("label", wHouseInfo.getFullName());
            options.add(option);
        }
        return ResponseUtil.okList(options);
    }
    //查询某个房屋下的房间
    @GetMapping("/optionsHouseToHome")
    public Object options(@RequestParam(defaultValue = "houseId") String houseId) {
        List<WRoomInfo> wRoomInfos = wHouseService.queryRoomByHouseId(houseId);
        List<Map<String, Object>> options = new ArrayList<>(wRoomInfos.size());
        for (WRoomInfo wRoomInfo : wRoomInfos) {
            Map<String, Object> option = new HashMap<>(2);
            option.put("value", wRoomInfo.getId());
            option.put("label", wRoomInfo.getRoomFullName());
            options.add(option);
        }
        return ResponseUtil.okList(options);
    }
}
